package com.umeng.community.example.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianxx.draw.R;
import com.qianxx.base.MyAdapter;
import com.qianxx.base.utils.MyBitmapUtil;
import com.qianxx.base.utils.ScreenUtils;
import com.umeng.community.example.NewInfoDetailAty;
import com.umeng.community.example.bean.NewInfo;

import java.util.List;

public class NewInfoAdapter extends MyAdapter<NewInfo,NewInfoAdapter.UserInfoViewHolder> {

	public NewInfoAdapter(Context context) {
		super(context);
	}

	@Override
	public void setData(List<NewInfo> list) {
		super.setData(list);
	}

	public class UserInfoViewHolder extends MyAdapter.ViewHolder {//必须继承,下面findViewHolder才可以获取到TestViewHolder
		ImageView newImg;
		TextView name;
		TextView description;
		View item_layout;
	}

	@Override
	protected UserInfoViewHolder findViewHolder(View view) {
		UserInfoViewHolder holder = new UserInfoViewHolder();
		holder.newImg = (ImageView)view.findViewById(R.id.newImg);
		holder.name = (TextView)view.findViewById(R.id.name);
		holder.description = (TextView)view.findViewById(R.id.description);
		holder.item_layout = view.findViewById(R.id.item_layout);
		return holder;
	}

	@Override
	protected void setViewDisplay(int position, NewInfo item,UserInfoViewHolder holder) {
		MyBitmapUtil.getInstance(mContext).setBitmap(holder.newImg,item.getBannerImg());
		setBannerHeight(holder.newImg);
		holder.name.setText(item.getName());
		holder.description.setText(item.getDescription());
	}

	@Override
	protected void setClickListener(int position, final NewInfo item,UserInfoViewHolder holder) {
		holder.item_layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent mIntent = new Intent(mContext, NewInfoDetailAty.class);
				Bundle mBundle = new Bundle();
				mBundle.putSerializable("newInfo", item);
				mIntent.putExtras(mBundle);
				mContext.startActivity(mIntent);
			}
		});
	}

	@Override
	protected int getLayoutId() {
		return R.layout.newinfo_adapter;
	}

	/**
	 * 设置图片的高度，让宽：高=1.8：1
	 */
	private int setBannerHeight(View layout) {
		int screenWitdh = ScreenUtils.getScreenWitdh(mContext);
		int width = screenWitdh;
		int height = (int) (width / 1.8);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
				height);
		layout.setLayoutParams(params);
		return height;
	}

}
